package com.lecture.java8.node;

import java.util.List;

/**
 * @author luyn
 * @version 1.0
 * @description 森林节点归并类
 * @date 2022/7/4 16:54
 */
public class ForestNodeMerger {

    public static <T extends INode> List<T> merge(List<T> items) {
        ForestNodeManager<T> forestNodeManager = new ForestNodeManager<>(items);
        items.forEach(forestNode -> {
            System.out.println(forestNode);
            if (forestNode.getParentId() != null && forestNode.getParentId() != 0 ) {
                INode node = forestNodeManager.getTreeNodeAt(forestNode.getParentId());
                if (node != null) {
                    node.getChildren().add(forestNode);
                } else {
                    forestNodeManager.addParentId(forestNode.getId());
                }
            }
        });
        return forestNodeManager.getRoot();
    }
}
